package com.ccl.iot.huiju;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;


public class CustomTextView extends View {
    private String mTextMsg = "让 购 物 便 捷";
    private Paint mPaint;
    private Paint mTextPaint;
    private Path mPath;


    private int mHeight = 116;
    private int mWidth = 110;
    private int mPaddingTop;
    private int mPaddingLeft;
    private int mArcWidth;
    private int mArcHeight;
    private int mTextOffset;
    private int mTextSize;

    public CustomTextView(Context context) {
        this(context,null);
    }


    public CustomTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
//        Log.e("6666","initView");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = width;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = height;
        }
        setMeasuredDimension(mWidth, mHeight);

        mPaddingTop = mHeight / 6;
        mPaddingLeft = mWidth / 6;
        mArcWidth = mWidth -  mPaddingLeft;
        mArcHeight = mHeight -  mPaddingTop;

        mTextOffset = mPaddingTop / 4;
        mTextSize = mPaddingTop - mTextOffset;


//        Log.e("6666","onMeasure--mWidth:" + mWidth + "--mHeight:" + mHeight);
    }

    private void initView() {

        mPaint = new Paint();
        mPaint.setColor(Color.TRANSPARENT);
        mPaint.setStrokeWidth(4);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setStrokeWidth(2);


    }

    public void setText(String msg,int color){
        mTextMsg = msg;
        mTextPaint.setColor(color);
        postInvalidate();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        Log.e("6666","onDraw");

        RectF rect =  new RectF(mPaddingLeft, mPaddingTop, mArcWidth, mArcHeight);

//        Log.e("6666","mWidth:" + mWidth + "--mHeight:" + mHeight );
        mPath = new Path();
        mPath.addArc(rect, 180, 180);
        //背景白色

        //右边开始绘制，对齐
        mTextPaint.setTextAlign(Paint.Align.LEFT);
        mTextPaint.setTextSize(mTextSize);

        //绘制路径
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawPath(mPath, mPaint);

        //绘制文字
        mTextPaint.setStyle(Paint.Style.FILL);
        canvas.drawTextOnPath(mTextMsg, mPath, -mTextOffset, 0, mTextPaint);

    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }
}
